package org.helldorado.companybuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import org.helldorado.companybuilder.R;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class ModelXmlPullParser {

	static final String KEY_SITE = "site";
	static final String KEY_NAME = "name";
	static final String KEY_LINK = "link";
	static final String KEY_ABOUT = "about";
	static final String KEY_IMAGE_URL = "image";

	public static Map<Integer, Model> getModelsFromFile(Context ctx) {

		// List of Models that we will return
		Map<Integer, Model> models;
		models = new HashMap<Integer,Model>();

		// temp holder for current Model while parsing
		Model curModel = null;
		// temp holder for current text value while parsing
		String curText = "";

		try {
			XmlPullParser xpp0= ctx.getResources().getXml(R.xml.hd_models);
			
			int eventType0 = xpp0.getEventType();

			// Loop through pull events until we 4reach END_DOCUMENT
			while (eventType0 != XmlPullParser.END_DOCUMENT) {
				String tagname0 = xpp0.getName();
				
				// React to different event types appropriately
				switch (eventType0) {
				case XmlPullParser.START_TAG:
					if (tagname0.equalsIgnoreCase("model")) {
						// If we are starting a new <site> block we need
						//a new Model object to represent it
						curModel = new Model();
						Integer points = Integer.parseInt(xpp0.getAttributeValue(null, "points"));
						String title = xpp0.getAttributeValue(null, "title");
						String subtitle = xpp0.getAttributeValue(null, "subtitle");
						Integer faction = Integer.parseInt(xpp0.getAttributeValue(null, "faction"));
						Integer rank = Integer.parseInt(xpp0.getAttributeValue(null, "rank"));
						Integer species = Integer.parseInt(xpp0.getAttributeValue(null, "species"));
						Integer gender = Integer.parseInt(xpp0.getAttributeValue(null, "gender"));
						Integer religion = Integer.parseInt(xpp0.getAttributeValue(null, "religion"));
						Integer infernalist = Integer.parseInt(xpp0.getAttributeValue(null, "infernalist"));
						String limit = xpp0.getAttributeValue(null, "availability");
						
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						curModel.setName(title);
						curModel.setSubtitle(subtitle);
						curModel.setLink("http://stackoverflow.com/");
						curModel.setId(id);
						curModel.setFaction(faction);
						curModel.setRank(rank);
						curModel.setSpecies(species);
						curModel.setGender(gender);
						curModel.setReligion(religion);
						curModel.setInfernalist(infernalist);
						curModel.setLimit(limit);					
						curModel.setPoints(points);
						
						//BitmapFactory.decodeFile(uri);
						//String imgUrl = "assets://" + faction + ".jpg";
						int resId = ctx.getResources().getIdentifier("i" + String.valueOf(id), "drawable", "org.helldorado.companybuilder");
						String imgUrl = "drawable://" + String.valueOf(resId);
						curModel.setImgUrl(imgUrl);
									
					}
					if (tagname0.equalsIgnoreCase("profile")) {
						curModel.MVT = xpp0.getAttributeValue(null, "MVT");
						curModel.SHS = xpp0.getAttributeValue(null, "SHS");
						curModel.CBT = xpp0.getAttributeValue(null, "CBT");
						curModel.DEF = xpp0.getAttributeValue(null, "DEF");
						curModel.PR = xpp0.getAttributeValue(null, "PR");
						curModel.LP = xpp0.getAttributeValue(null, "LP");
						curModel.FTH = xpp0.getAttributeValue(null, "FTH");
						curModel.CMD = xpp0.getAttributeValue(null, "CMD");
						
						curModel.MVTW = xpp0.getAttributeValue(null, "MVTW");
						curModel.SHSW = xpp0.getAttributeValue(null, "SHSW");
						curModel.CBTW = xpp0.getAttributeValue(null, "CBTW");
						curModel.DEFW = xpp0.getAttributeValue(null, "DEFW");
						curModel.LPW = xpp0.getAttributeValue(null, "LPW");
						curModel.FTHW = xpp0.getAttributeValue(null, "FTHW");
						curModel.setLifePoints(Integer.parseInt(curModel.LP)-3);
						curModel.setMaxLifePoints(Integer.parseInt(curModel.LP));
						curModel.setWoundedLifePoints(Integer.parseInt(curModel.LPW));
					}
					if (tagname0.equalsIgnoreCase("abilities")) {
						if(xpp0.next() == XmlPullParser.TEXT) {
							String str = xpp0.getText();
							
							if (str != null)
							{
								List<String> abilitiesList = Arrays.asList(str.split(","));
								List<TIPD> tipds = new ArrayList<TIPD>();
								for (String ab : abilitiesList) {
									Integer abInt = Integer.parseInt(ab);
									tipds.add(Ability.getAbilityById(abInt));
								}
								curModel.setAbilities(tipds);
							}
						}
					}
					if (tagname0.equalsIgnoreCase("orders")) {
						if(xpp0.next() == XmlPullParser.TEXT) {
							String str = xpp0.getText();
							if (str != null)
							{
								List<String> ordersList = Arrays.asList(str.split(","));
								List<TIPD> tipds = new ArrayList<TIPD>();
								for (String ab : ordersList) {
									Integer abInt = Integer.parseInt(ab);
									tipds.add(Order.getOrderById(abInt));
								}
								curModel.setOrders(tipds);
							}
						}
					}
					if (tagname0.equalsIgnoreCase("spells")) {
						if(xpp0.next() == XmlPullParser.TEXT) {
							String str = xpp0.getText();
							if (str != null)
							{
								List<String> spellsList = Arrays.asList(str.split(","));
								List<TIPD> tipds = new ArrayList<TIPD>();
								for (String ab : spellsList) {
									Integer abInt = Integer.parseInt(ab);
									tipds.add(Spell.getSpellById(abInt));
								}
								curModel.setSpells(tipds);
							}
						}
					}
					if (tagname0.equalsIgnoreCase("auras")) {
						if(xpp0.next() == XmlPullParser.TEXT) {
							String str = xpp0.getText();
							if (str != null)
							{
								List<String> aurasList = Arrays.asList(str.split(","));
								List<TIPD> tipds = new ArrayList<TIPD>();
								for (String ab : aurasList) {
									Integer abInt = Integer.parseInt(ab);
									tipds.add(Aura.getAuraById(abInt));
								}
								curModel.setAuras(tipds);
							}
						}
					}
					if (tagname0.equalsIgnoreCase("weapons")) {
						if(xpp0.next() == XmlPullParser.TEXT) {
							String str = xpp0.getText();
							if (str != null)
							{
								List<String> weaponList = Arrays.asList(str.split(","));
								List<Weapon> weapons = new ArrayList<Weapon>();
								for (String ab : weaponList) {
									Integer abInt = Integer.parseInt(ab);
									Weapon weapon = Weapon.getWeaponById(abInt);
									if (weapon.getId() != 0){
										weapons.add(weapon);
									}
								}
								curModel.setWeapons(weapons);
							}
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if (tagname0.equalsIgnoreCase("model")) {
						models.put(curModel.getId(), curModel);	
					}
					break;

				default:
					break;
				}
				//move on to next iteration
				eventType0 = xpp0.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// return the populated list.
		return models;
	}
}
